package red.mlz.smsservice.service.smstask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.mlz.common.entity.sms.smstask.SmsTask;
import red.mlz.smsservice.mapper.smstask.SmsTaskMapper;

import java.util.List;

@Service
public class SmsTaskService {

    @Autowired
    private SmsTaskMapper smsTaskMapper;

    public boolean updateStatusWithOptimisticLock(Long id, Integer oldStatus, Integer newStatus) {
        int updateTime = (int) (System.currentTimeMillis() / 1000);
        int rows = smsTaskMapper.updateStatusWithOptimisticLock(id, oldStatus, newStatus, updateTime);
        return rows > 0;
    }

    public SmsTask getById(Long smsTaskId) {

        return smsTaskMapper.getById(smsTaskId);
    }

    public SmsTask extractById(Long smsTaskId) {

        return smsTaskMapper.extractById(smsTaskId);
    }

    public Integer insert(SmsTask entity) {

        return smsTaskMapper.insert(entity);
    }

    public Integer update(SmsTask entity) {

        return smsTaskMapper.update(entity);
    }

    public Integer delete(Long id, Integer updateTime) {

        return smsTaskMapper.delete(updateTime, id);
    }

    public List<SmsTask> findByStatus(Integer status) {
        return smsTaskMapper.findByStatus(status);
    }

    public void updateStatus(Long id, int status) {
        smsTaskMapper.updateStatus(id, status, (int) (System.currentTimeMillis() / 1000));
    }

    public List<SmsTask> findByStatusWithPaging(Integer status, int page, int pageSize) {
        int offset = page * pageSize;
        return smsTaskMapper.findByStatusWithPaging(status, offset, pageSize);
    }

    public void updateRetryCount(Long id, int retryCount) {
        int updateTime = (int) (System.currentTimeMillis() / 1000);
        smsTaskMapper.updateRetryCount(id, retryCount, updateTime);
    }
}
